﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using MalaCSharp;
using System.Security.Cryptography;

namespace Tester
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //String inFile = @"C:\Program Files Not Installed\MaLa174\MaLa174\all games.mlg";
                String inFile = @"C:\Users\Mike\Desktop\Driving.mlg";
                String outFile = @"C:\Program Files Not Installed\MaLa174\MaLa174\DrivingTest.mlg";

                MalaGameList gameListIn = new MalaGameList();
                gameListIn.Load(inFile);

                MalaGameList gameListOut = new MalaGameList();
                gameListOut.Name = gameListIn.Name;
                gameListOut.Author = gameListIn.Author;
                gameListOut.Comment = gameListIn.Comment;
                gameListOut.Creation = gameListIn.Creation;
                gameListOut.Modification = gameListIn.Modification;
                gameListOut.WriteProtected = gameListIn.WriteProtected;
                gameListOut.SortField = gameListIn.SortField;

                // index: 127

                for (int i = 0; i < 128; i++)
                {
                    if (i >= gameListIn.Games.Count)
                    {
                        Console.WriteLine("EOF");
                        break;
                    }

                    gameListOut.Games.Add(new MalaGame(gameListIn.Games[i]));
                }

                Console.WriteLine(gameListOut.Games[gameListOut.Games.Count - 1].ToString());
                gameListOut.Games[gameListOut.Games.Count - 1].Copy(gameListOut.Games[gameListOut.Games.Count - 2]);
                Console.WriteLine(gameListOut.Games[gameListOut.Games.Count - 1].ToString());

                gameListOut.Save(outFile);
                Console.WriteLine("done");

                /*

                //MalaGameList gameList2 = new MalaGameList();
                //gameList2.Load(outFile);

                //Console.WriteLine("Equality Match: " + gameList1.Equals(gameList2).ToString());


                Console.WriteLine("Name: " + gameList1.Name);
                Console.WriteLine("Author: " + gameList1.Author);
                Console.WriteLine("Comment: " + gameList1.Comment);
                Console.WriteLine("Creation: " + gameList1.Creation);
                Console.WriteLine("Modification: " + gameList1.Modification);
                Console.WriteLine("WriteProtected: " + gameList1.WriteProtected);
                Console.WriteLine("SortField: " + gameList1.SortField);
                Console.WriteLine("Game Count: " + gameList1.Games.Count);
                
                Console.ReadLine();

                foreach (MalaGame game in gameList1.Games)
                {
                    Console.WriteLine("");
                    Console.WriteLine(game.ToString());
                    Console.ReadLine();

                }
           
                /*
                List<Byte> inBytes = new List<byte>();
                using (FileStream stream = new FileStream(inFile, FileMode.Open, FileAccess.Read))
                {
                    while (true)
                    {
                        int iByte = stream.ReadByte();
                        if (iByte == -1)
                            break;
                        else
                            inBytes.Add((byte)iByte);
                    }
                }

                List<Byte> outBytes = new List<byte>();
                using (FileStream stream = new FileStream(outFile, FileMode.Open, FileAccess.Read))
                {
                    while (true)
                    {
                        int iByte = stream.ReadByte();
                        if (iByte == -1)
                            break;
                        else
                            outBytes.Add((byte)iByte);
                    }
                }

                if (inBytes.Count != outBytes.Count)
                    Console.WriteLine("Length changed.");

                bool byteMatch = true;

                int minLength = Math.Min(inBytes.Count, outBytes.Count);                               
                for (int i = 0; i < minLength; i++)
                {
                    if (inBytes[i] != outBytes[i])
                    {
                        byteMatch = false;
                        break;
                    }
                }

                Console.WriteLine("Byte match: " + byteMatch.ToString());*/
            }
            catch (Exception err)
            {
                Console.WriteLine(err.ToString());
            }

            Console.ReadLine();
        }

        private static byte[] GetMd5(String filename)
        {
            using (var md5 = MD5.Create())
            {
                using (var stream = File.OpenRead(filename))
                {
                    return md5.ComputeHash(stream);
                }
            }
        }
    }
}
